//
// Created by nvidia on 2021/6/8.
//

#include "plot.h"

Plot::Plot(float num) {
    plt::ion();
    plt::figure_size(512, 512);
    start = cv::getTickCount();
    this->num = num;
}

void Plot::record(double data, std::string name) {
    Debug(data)
    bool isFind = false;
    for(int i=0;i<names.size();i++){
        if(name==names[i]){
            datas[i].push_back(data);
            tick[i].push_back((cv::getTickCount()-start)/cv::getTickFrequency());
            isFind = true;
        }
    }
    if(isFind==false){
        names.push_back(name);
        datas.push_back(std::vector<double>());
        datas[datas.size()-1].push_back(data);
        tick.push_back(std::vector<double>());
        tick[tick.size()-1].push_back((cv::getTickCount()-start)/cv::getTickFrequency());
    }
    for(int i=0;i<names.size();i++){
        if(datas[i].size()>num){
            datas[i].erase(datas[i].begin());
            tick[i].erase(tick[i].begin());
        }
    }
}

void Plot::show() {
    for(int i=0;i<names.size();i++){
        plt::named_plot(names[i], tick[i], datas[i]);
    }
    plt::legend();
    plt::pause(0.001);
    plt::clf();
}
